import Dialog from '@vant/weapp/dialog/dialog';
import {
  reqConsumerDeviceBatchDel,
  reqConsumerDeviceUpdate
} from '../../../../request/api.js'
import {
  showMsg
} from '../../../../utils/config.js'
const app = getApp()

Page({
    data: {
      gonzhonghao1: '/statics/images/gaojing/yangan.png',
      yangan: './yangan2.png',
      showInputDialog: false,
      editInfo:{}
    },
  
    onLoad(options) {
      console.log(options,'options');
      this.setData({
        editInfo:JSON.parse(options.editInfo)
      })
    },
  
    onShow() {
  
    },
    clickDelete() {
      Dialog.confirm({
          title: '温馨提示',
          message: '确认要移除此设备吗？',
        })
        .then(async () => {
          const res = await reqConsumerDeviceBatchDel({
            deviceCodeList:[this.data.editInfo.deviceCode]
          })
          if (res.code != 20000) return showMsg(res.message);
          showMsg("移除成功")
          setTimeout(() => {
            wx.switchTab({
            url: '/pages/index/index',
          })
          app.globalData.isUpdateCIndex = true
          wx.setStorageSync('isUpdateCIndex', true);
          }, 500)
        })
        .catch(() => {});
    },
    onEditName(){
      this.setData({
        showInputDialog: true
      })
    },
    inputDialogHandle(e) {
      console.log(e);
      const {
        actionType,
        value
      } = e.detail
      if (actionType === 'confirm') {
        if (!value) return showMsg("请输入设备名称")
        this.setData({
          "deviceName": value
        })
        this.getConsumerDeviceUpdate()
       
      } else {
        this.setData({
          showInputDialog: false
        })
      }
    },
   async getConsumerDeviceUpdate(){
      const res = await reqConsumerDeviceUpdate({
        deviceCode:this.data.editInfo.deviceCode,
        deviceName:this.data.deviceName,
        roomId:this.data.editInfo.roomId
      })
      console.log(res, 'res');
      if (res.code !== 20000) return showMsg(res.message)
      this.setData({
        showInputDialog: false,
        "editInfo.deviceName":this.data.deviceName
      })
      app.globalData.deviceInfo = this.data.editInfo
      wx.setStorageSync('deviceInfo',this.data.editInfo);
      app.globalData.isUpdateCIndex = true
      wx.setStorageSync('isUpdateCIndex', true);
    },
    onChangeRoom() {
      this.setData({
        showRoomSelection: true
      })
    },
    async confirmRoomSelection(e) {
      const {
        action,
        roomInfo
      } = e.detail
      console.log(e,'e');
      if (action == 'confirm') {
        const res = await reqConsumerDeviceUpdate({
          deviceCode:this.data.editInfo.deviceCode,
          deviceName:this.data.editInfo.deviceName,
          roomId: roomInfo.id,
        })
        console.log(res, 'res');
        if (res.code !== 20000) return showMsg(res.message)
        this.setData({
          showRoomSelection: false,
          "editInfo.roomId":roomInfo.id,
          "editInfo.roomName":roomInfo.roomName
        })
        app.globalData.deviceInfo = this.data.editInfo
        wx.setStorageSync('deviceInfo',this.data.editInfo);
        app.globalData.isUpdateCIndex = true
        wx.setStorageSync('isUpdateCIndex', true);
      } else {
        this.setData({
          showRoomSelection: false
        })
      }
    },
  })